package com.hafu.view.RuKu;

import com.hafu.model.RuKuMain;
import com.hafu.service.RuKuService;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;

public class QueryRuKuFrm extends JFrame {
    private RuKuService ruKuService = new RuKuService();


    public QueryRuKuFrm() {
        this.initComponents();
        DefaultTableModel defaultTableModel = (DefaultTableModel)this.table1.getModel();
        defaultTableModel.setRowCount(0);
        ArrayList<RuKuMain> ruKuList = this.ruKuService.getRuKuList(new RuKuMain());
        Iterator var3 = ruKuList.iterator();

        while(var3.hasNext()) {
            RuKuMain ruKu = (RuKuMain)var3.next();
            Object[] rowData = new Object[]{ruKu.getId(), ruKu.getRukuName(), ruKu.getPrice(), ruKu.getGysName(), ruKu.getRkDate()};
            defaultTableModel.addRow(rowData);
        }

    }

    private void button1(ActionEvent e) {
        this.textField2.setText("");
        String text = this.textField1.getText();
        DefaultTableModel defaultTableModel = (DefaultTableModel)this.table1.getModel();
        defaultTableModel.setRowCount(0);
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRukuName(text);
        ArrayList<RuKuMain> ruKuList = this.ruKuService.getRuKuList(ruKuMain);
        if (ruKuList.size() > 0) {
            Iterator var6 = ruKuList.iterator();

            while(var6.hasNext()) {
                RuKuMain ruKu = (RuKuMain)var6.next();
                Object[] rowData = new Object[]{ruKu.getId(), ruKu.getRukuName(), ruKu.getPrice(), ruKu.getGysName(), ruKu.getRkDate()};
                defaultTableModel.addRow(rowData);
            }
        } else {
            JOptionPane.showMessageDialog((Component)null, "没有找到相关采购入库单");
        }

    }

    private void table1MouseClicked(MouseEvent e) {
        int row = this.table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel)this.table1.getModel();
        int chooseId = (Integer)dtm.getValueAt(row, 0);
        String name = (String)dtm.getValueAt(row, 1);
        this.textField2.setText(name);
    }

    private void button2(ActionEvent e) {
        if (this.table1.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog((Component)null, "请选择要修改的采购入库单");
        } else {
            String name = this.textField2.getText();
            int row = this.table1.getSelectedRow();
            DefaultTableModel dtm = (DefaultTableModel)this.table1.getModel();
            int chooseId = (Integer)dtm.getValueAt(row, 0);
            RuKuMain ruKuMain = new RuKuMain();
            ruKuMain.setId(chooseId);
            ruKuMain.setRukuName(name);
            ArrayList<RuKuMain> ruKuList = this.ruKuService.getRuKuList(ruKuMain);
            if (ruKuList.size() > 1) {
                JOptionPane.showMessageDialog((Component)null, "采购入库单不允许重名");
            } else if (ruKuList.size() == 1 && ((RuKuMain)ruKuList.get(0)).getId() != chooseId) {
                JOptionPane.showMessageDialog((Component)null, "采购入库单不允许重名");
            } else {
                Boolean aBoolean = this.ruKuService.updateRuKu(ruKuMain);
                if (aBoolean) {
                    JOptionPane.showMessageDialog((Component)null, "修改成功");
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog((Component)null, "修改失败");
                }

            }
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - aaaa
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        button2 = new JButton();

        //======== this ========
        setTitle("\u67e5\u8be2\u8ba2\u5355\u4fe1\u606f");
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5b66\u751f\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, null, null},
                    {null, null, null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u603b\u4ef7", "\u8001\u5e08\u540d\u5b57", "\u4e0b\u5355\u65f6\u95f4"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5b66\u751f\u540d\u79f0");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(42, 42, 42)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 730, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label1)
                            .addGap(60, 60, 60)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
                            .addGap(133, 133, 133)
                            .addComponent(button1))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
                            .addGap(60, 60, 60)
                            .addComponent(button2)))
                    .addContainerGap(151, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button1))
                    .addGap(34, 34, 34)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE)
                    .addGap(27, 27, 27)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2))
                    .addContainerGap(36, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }



    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - aaaa
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
